<?php
/**
 * RedXenon Framework
 * 
 * LICENSE
 * 
 * This source file is subject to the New BSD license that is bundled
 * with this package in the file LICENSE.txt
 * A copy of this license can be found in the world-wide-web at this URL:
 * http://www.redxenon.com/license/new-bsd-license.txt
 *
 * RedXenon Curl (RXCurl.php)
 * Defines a curl interface to configure and manage the behaviour 
 * of HTTP requests though the PHP Curl Extension
 * 
 * IMPORTANT: The PHP Curl Extension must be installed in your
 * system in order to be able to use this class  
 * 
 * @category RedXenon
 * @package RedXenon_Curl
 * @copyright Copyright (c) 2012 Red Xenon (http://www.redxenon.com)
 * @version $Id: RXCurl.php 1.0 2012-06-02 $ 
 */
class RXCurl{
	
	private $curl;
	private $bufferSize;
	private $post;
	private $returnTransfer;
	private $timeout;	
	private $requestHeaders;
	private $requestEncoding;
	private $content;
	private $info;
	private $cookie;
	private $responseHttpCode;
	
	public function __construct($url){
		try{
			$this->curl = curl_init();
			curl_setopt($this->curl, CURLOPT_URL, $url);
			curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, TRUE);
			
		}catch(Exception $e){
			throw new Exception("Error on creating Curl Instance: ".$e->getMesage());	
		}
	}
	
	public function setPost(){
		$this->post = TRUE;
	}
	
	public function setBufferSize($size){
		$this->bufferSize = $size;
	}
	
	public function setReturnTransfer(){
		$this->returnTransfer = TRUE;
	}
	
	public function setTimeout($timeout){
		$this->timeout = $timeout;
	}
	
	public function getInfo(){
		return $this->info;
	}
	
	public function getContent(){
		return $this->content;
	}
	
	public function setRequestHeaders($headers){
		$this->requestHeaders = $headers;
	}
	
	public function setUserAgent($userAgent){
		$this->userAgent = $userAgent;
	}
	
	public function setRequestEncoding($encoding){
		$this->requestEncoding = $encoding;
	}
	
	public function setCookie($cookie){
		$this->cookie = $cookie;
	}
	
	public function getResponseContentType(){
		return @$this->responseContentType;
	}
	
	public function getResponseCharsetEncoding(){
		return @$this->responseCharsetEncoding;
	}
	
	public function getResponseHttpCode(){
		return @$this->responseHttpCode;
	}
	
	public function execute(){
		if($this->post)
			curl_setopt($this->curl, CURLOPT_POST, 1);
		if($this->returnTransfer)
			curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
		if($this->bufferSize && is_numeric($this->bufferSize))
			curl_setopt($this->curl, CURLOPT_BUFFERSIZE, $this->bufferSize);
		if($this->timeout && is_numeric($this->timeout))
			curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
		if($this->userAgent)
			curl_setopt($this->curl, CURLOPT_USERAGENT, $this->userAgent);
		if($this->cookie){
			curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookie);
			curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookie);
		}
		
		curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0);			
		
		if($this->requestEncoding)
			curl_setopt($this->curl, CURLOPT_ENCODING, $this->requestEncoding);
		else
			curl_setopt($this->curl, CURLOPT_ENCODING, "gzip,deflate");
			
		if($this->requestHeaders){
			curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->requestHeaders);
		}else{
			$requestHeaders = array(	"Accept:text/plain,text/html,text/xml,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
															"Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3", 
															"Accept-Language:es-ES,es;q=0.8",
															"Cache-Control:max-age=0",
															"Connection:keep-alive"	, 
															"Expect:"
														);
			curl_setopt($this->curl, CURLOPT_HTTPHEADER, $requestHeaders);
		}
				
		$this->content = curl_exec($this->curl);

		if($this->content){		
			$this->info = curl_getinfo($this->curl);
			$contentType =  explode(";",$this->info["content_type"]);
			$this->responseContentType = (count($contentType) > 0) ? trim($contentType[0]) : $this->info["content_type"];
			$this->responseHttpCode = $this->info["http_code"];
			if(@$contentType[1] && preg_match('/charset=/i', $contentType[1]))
				$this->responseCharsetEncoding = trim(str_ireplace('charset=', '', $contentType[1]));
			curl_close($this->curl);
			return TRUE;
		}else{
			curl_close($this->curl);
			return FALSE;
		}
	}
}
?>